package com.autoai.animation.demo;

import android.app.Instrumentation;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "AnimationDemo";
    FirstFragment firstFragment = new FirstFragment();
    SecondFragment secondFragment = new SecondFragment();
    ThirdFragment thirdFragment = new ThirdFragment();
    FourthFragment fourthFragment = new FourthFragment();

    public Handler getHandler() {
        return handler;
    }

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_main_activity);
        handler = new Handler(Looper.getMainLooper());
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.main_wallpaper, firstFragment, "FirstFragment");
        transaction.commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.addToBackStack("SecondFragment");
        transaction.replace(R.id.main_pop_layout, secondFragment, "SecondFragment");
        transaction.commitAllowingStateLoss();
    }

    public void sentBack(View view){
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    Instrumentation inst=new Instrumentation();
                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }).start();
    }

    public void showHomeFragment(View view) {
        //TODO Activity切换
//        secondFragment.layoutOut();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Intent intent = new Intent(MainActivity.this, ViewPagerActivity.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.anim_right_in, R.anim.launcher_out);
//            }
//        }, 800);
        //TODO Fragment切换
        List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
        Log.e("ShowHomeFragment","FragmentList:"+fragmentList);
        if (fragmentList.contains(thirdFragment)) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.anim_right_in,R.anim.anim_left_out,R.anim.anim_left_in,R.anim.anim_right_out);
            transaction.addToBackStack("fourth");
            transaction.replace(R.id.main_fragment_layout, fourthFragment, "FourthFragment");
            transaction.commitAllowingStateLoss();
        } else {
            secondFragment.layoutOut();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.addToBackStack("third");
            transaction.replace(R.id.main_fragment_layout, thirdFragment, "ThirdFragment");
            transaction.commitAllowingStateLoss();
//            handler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    WallpaperControl.getInstance().changeAnim();
//                }
//            }, 1000);
        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();
    }


    @Override
    protected void onStop() {
        super.onStop();
    }
}